//
//  ViewController.m
//  StudyDemo01
//
//  Created by 心晨 on 2024/5/9.
//

#import "ViewController.h"
#import "GPThreadManger.h"
#import "SDTestViewController.h"
#import "XCObserver.h"
#import "Masonry.h"
#import "SDWebImage.h"
#import <objc/runtime.h>
#import "GPThread.h"
#import "ViewController+Test.h"
#import "GPSwizzled.h"
#import "YYDispatchQueuePool.h"
#import <SDWebImage/SDInternalMacros.h>

@interface ViewController (){
#if SD_UIKIT
    SD_LOCK_DECLARE(_weakCacheLock); // a lock to keep the access to `weakCache` thread-safe
#endif
}

@property (nonatomic, strong) XCObserver *observer;

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, copy) NSString *key;

@property (nonatomic, copy) void (^Test01)(void);

@property (nonatomic, strong) NSMutableArray *testList;

@property (nonatomic, strong) GPThread *workerThread;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self threadManger];

    [self addButton];
    
    self.observer = [[XCObserver alloc] initWithObserver:self forKeyPath:@"index" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil block:^(NSDictionary * _Nonnull change) {
        NSLog(@"change %@", change);
    }];
            
    
    NSArray *array0 = @[@1, @2, @2, @6, @4];
    NSOrderedSet *set = [[NSOrderedSet alloc] initWithArray:array0];
    NSArray *tempArray = set.array;
    
    NSLog(@"change ");
    self.testList = [NSMutableArray new];
    [self.testList addObject:@"1"];
    NSMutableArray *testList01 = [NSMutableArray new];
    [testList01 addObject:@"1"];
    self.Test01 = ^{
        [self.testList addObject:@"2"];
        [testList01 addObject:@"22"];
        NSLog(@"change ");
    };
    
    _workerThread = [[GPThread alloc] init];
    [_workerThread start];
    [_workerThread performSelector:@selector(doWork) onThread:_workerThread withObject:nil waitUntilDone:NO];

    //执行foo函数
    [self performSelector:@selector(foo:)];
    [self performSelector:@selector(fooMethodA)];

    GPSwizzled *swizzled = [GPSwizzled new];
    [swizzled methodA];
    [swizzled methodB];
    
}



- (void)addButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"跳转" forState:UIControlStateNormal];
    [button setBackgroundColor:UIColor.cyanColor];
    [button addTarget:self action:@selector(closeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view.mas_centerY);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.height.mas_offset(120);
    }];
    
//    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
//    [button1 setTitle:@"跳转" forState:UIControlStateNormal];
//    [button1 setBackgroundColor:UIColor.redColor];
//    [button1 addTarget:self action:@selector(closeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button1];
//    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.view.mas_centerY);
//        make.width.height.mas_offset(120);
//    }];
    
//    NSString *url = @"https://webcdn.closeli.cn/eyeplus/ads/ads_20240429_297735dfaf064f0f8e4ddc9c0d261cb6_584x834.png";
//    UIImageView *imageView = [[UIImageView alloc] init];
//    [self.view addSubview:imageView];
//    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.view.mas_centerY);
//        make.centerX.equalTo(self.view.mas_centerX);
//    }];
//    
//    [imageView sd_setImageWithURL:[NSURL URLWithString:url]];、
    
    // key: GPSwizzled value: key:self value:Observer
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationA) name:@"GPSwizzled" object:self];
    // key: GPSwizzled value: Observer
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationB) name:@"GPSwizzled" object:nil];
    
//    dispatch_queue_t queue = dispatch_queue_create("xxQueue", DISPATCH_QUEUE_CONCURRENT);
//    dispatch_apply(100000, queue, ^(size_t t) {
//            NSLog(@"the current thread = %@, t = %ld", NSThread.currentThread, t);
//    });
    
    // Get a serial queue from global queue pool
//    dispatch_queue_t queue = YYDispatchQueueGetForQOS(NSQualityOfServiceUtility);

    // Create a serial queue pool
    
}

- (void)closeButtonClicked {
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"GPSwizzled" object:self];
//    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"GPSwizzled" object:nil];
//    return;
//    [_workerThread performSelector:@selector(doWork) onThread:_workerThread withObject:nil waitUntilDone:NO];
//    return;
//    if (self.Test01) {
//        self.Test01();
//    }
//    self.index = 3;
//    return;
    SDTestViewController *vc = [[SDTestViewController alloc] init];
    vc.view.backgroundColor = [UIColor whiteColor];
//    [self.navigationController pushViewController:vc animated:NO];
    [self presentViewController:vc animated:NO completion:nil];
    
    [self loadViewIfNeeded];
    
}

- (void)notificationA {
    NSLog(@"notificationA notificationA ");
}

- (void)notificationB {
    NSLog(@"notificationB notificationB ");
}

- (void)threadManger {
    
    SD_LOCK_INIT(_weakCacheLock);
        
    [[GPThreadManger shareInstance] dispatchQueuePriority];
    
    [[GPThreadManger shareInstance] serialQueue];
    
//    [[GPThreadManger shareInstance] concurrentQueue];
    
//    [[GPThreadManger shareInstance] dispatchGroup];
    
//    [[GPThreadManger shareInstance] dispatchGroupNotify];
    
//    [[GPThreadManger shareInstance] dispatchBarrierAsync];
    
}

- (void)test01:(NSString *)test {
    NSLog(@"1111");
}

- (void)test01:(NSString *)test test02:(NSString *)test02 {
    
}

+ (BOOL)resolveInstanceMethod:(SEL)sel {
    if (sel == @selector(foo:)) {
        Method method = class_getInstanceMethod(self, @selector(fooMethod));
        const char *types = method_getTypeEncoding(method);
        class_addMethod(self, sel, method_getImplementation(method), types);
        return YES;
    }
    if (sel == @selector(fooMethodA)) {
        return YES;
    }
    return [super resolveInstanceMethod:sel];
}

- (id)forwardingTargetForSelector:(SEL)aSelector {
    if (aSelector == @selector(serialQueue)) {
        return [GPThreadManger new];
    }
    return nil;
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    if ([NSStringFromSelector(aSelector) isEqualToString:@"serialQueue"]) {
        return [NSMethodSignature signatureWithObjCTypes:"v@:"];//签名，进入forwardInvocation
    }
    
    if (aSelector == @selector(fooMethodA)) {
        return [NSMethodSignature signatureWithObjCTypes:"v@:"];
    }
    
    return [super methodSignatureForSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    SEL sel = anInvocation.selector;
    SDTestViewController *test = [SDTestViewController new];
    GPThreadManger *p = [GPThreadManger new];
    if([p respondsToSelector:sel]) {
        [anInvocation invokeWithTarget:p];
    } else if ([test respondsToSelector:sel]) {
        [anInvocation invokeWithTarget:test];
    }
    else {
        [self doesNotRecognizeSelector:sel];
    }

}

- (void)fooMethod {
    NSLog(@"fooMethod fooMethod");
}

- (void)fooMethodB {
    NSLog(@"fooMethodB fooMethodB");
}


@end
